package cn.wolfcode.service.impl;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.meiju.AppointmentStatusEnum;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

//    private static final Logger log = LoggerFactory.getLogger(AppointmentServiceImpl.class);


    //后台客服帮用户的预约
    @Override
    public void save(Appointment appointment) {

        //使用hutools获取时间字符串
        String dateStr = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        //生成随机五位数
        String numbers = RandomUtil.randomNumbers(5);
        //设值流水号
        appointment.setAno(dateStr+numbers);
        appointment.setCreateTime(new Date());
        appointment.setStatus(AppointmentStatusEnum.PERFORM.getValue());
        appointmentMapper.insert(appointment);
        log.info("预约的流水单号是:{}",dateStr+numbers);
    }

    //页面用户的预约
    @Override
    public void save2(Appointment appointment) {
        //使用hutools获取时间字符串
        String dateStr = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        //生成随机五位数
        String numbers = RandomUtil.randomNumbers(5);
        //设值流水号
        appointment.setAno(dateStr+numbers);
        appointment.setCreateTime(new Date());
        appointment.setStatus(AppointmentStatusEnum.PEND.getValue());
        appointmentMapper.insert(appointment);
    }

    @Override
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> listAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public PageInfo<Appointment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"status,appointment_time"); //对下一句sql进行自动分页
        List<Appointment> appointments = appointmentMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Appointment>(appointments);
    }

    @Override
    public List<SystemDictionaryItem> selectBySn(String sn) {
         return  appointmentMapper.selectBySn(sn);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        appointmentMapper.updateStatus(id,status);
    }

    @Override
    public Appointment selectByAno(String appointmentAno) {
        return appointmentMapper.selectByAno(appointmentAno);
    }
}
